package com.gxsf.wechatbook.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gxsf.wechatbook.pojo.TokenList;
import com.gxsf.wechatbook.pojo.User;
import com.gxsf.wechatbook.repo.TokenListRepository;
import com.gxsf.wechatbook.service.UserService;
import com.gxsf.wechatbook.token.PassToken;
import com.gxsf.wechatbook.token.UserLoginToken;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class AuthenticationInterceptor implements HandlerInterceptor {
    @Autowired
    UserService userService;
    @Autowired
    TokenListRepository tokenListRepository;

    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object) throws Exception {
        String token = httpServletRequest.getHeader("token");
        if (!(object instanceof HandlerMethod)) {
            return true;
        } else {
            HandlerMethod handlerMethod = (HandlerMethod)object;
            Method method = handlerMethod.getMethod();
            if (method.isAnnotationPresent(PassToken.class)) {
                PassToken passToken = (PassToken)method.getAnnotation(PassToken.class);
                if (passToken.required()) {
                    return true;
                }
            }

            if (method.isAnnotationPresent(UserLoginToken.class)) {
                UserLoginToken userLoginToken = (UserLoginToken)method.getAnnotation(UserLoginToken.class);
                if (userLoginToken.required()) {
                    if (token == null) {
                        throw new RuntimeException("无token，请重新登录");
                    }

                    User user = null;
                    TokenList tokenList = this.tokenListRepository.findFirst1ByTokenNameAndStatusOrderByTokenIdDesc(token, 0L);
                    if (tokenList == null) {
                        throw new RuntimeException("未找到token信息");
                    }

                    String message = tokenList.getMessage();
                    JSONObject jsonObject = JSON.parseObject(message);
                    user = (User)JSON.toJavaObject(jsonObject, User.class);
                    if (user == null) {
                        throw new RuntimeException("用户不存在，请重新登录");
                    }

                    return true;
                }
            }

            return true;
        }
    }

    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    }

    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    }
}
